<?php
/**
 * Created Dec. 15, 2009
 * @author: Christopher Sheldahl
 */
class OMWPT_ConfigFileReader
{
	/**
	 * reads files in following format w/ newline at end of each line:
	 	and *optional* comments preceded by ';' on each line
	 	directory string	; comment this line
	 	filename
	 	splat path			
	 	convert path
	 	splat climate code
	*/


	public function read($configFilePath)
	{
		$sep = ";";
	 	$file = fopen($configFilePath, 'r');
	 	//read workingDirectory
	 	$currLine = fgets($file);
	 	$pos = strpos($currLine,$sep);
	 	//echo $currLine;
	 	if ( $pos === False )
	 	{
	 		$workingDirectory = $currLine;
	 	}
	 	else
	 	{
	 		//echo $pos;
	 		$tokens = preg_split("/" . $sep . "/", $currLine);
	 		$workingDirectory = $tokens[0];
	 	}
	 	//read transmitterFileName
	 	$currLine = fgets($file);
	 	$pos = strpos($currLine,$sep);
	 	if ( $pos === False )
	 	{
	 		$transmitterFileName = $currLine;
	 	}
	 	else
	 	{
	 		$tokens = preg_split("/" . $sep . "/", $currLine);
	 		$transmitterFileName = $tokens[0];
	 	}
	 	$currLine = fgets($file);
	 	
	 	//read splatPath
	 	$pos = strpos($currLine,$sep);
	 	//echo "splat path *** : " . $currLine;
	 	if ( $pos === False )
	 	{
	 		$splatPath= $currLine;
	 	}
	 	else
	 	{
	 		$tokens = preg_split("/" . $sep . "/", $currLine);
	 		$splatPath = $tokens[0];
	 	}
	 	
	 	//read convertPath
	 	$currLine = fgets($file);
	 	if ( $pos === False )
	 	{
	 		$convertPath= $currLine;
	 	}
	 	else
	 	{
	 		$tokens = preg_split("/" . $sep . "/", $currLine);
	 		$convertPath = $tokens[0];
	 	}
	 	
	 	//read climate
	 	$currLine = fgets($file);
	 	if ( $pos === False )
	 	{
	 		$climate= $currLine;
	 	}
	 	else
	 	{
	 		$tokens = preg_split("/" . $sep . "/", $currLine);
	 		$climate = $tokens[0];
	 	}
	 	$result = new OMWPT_ConfigParams();
	 	$result->climate = $climate;
	 	$result->workingDirectory = $workingDirectory;
	 	$result->transmitterFileName = $transmitterFileName;
	 	$result->splatPath = $splatPath;
	 	$result->convertPath = $convertPath;
		return $result;
		
		fclose($file);
	}
	
}

class OMWPT_ConfigParams
{
	var $workingDirectory; //string
	var $transmitterFileName; //if lrp file is "foo.lrp" then this is "foo"
	var $splatPath;  //path for splat directory
	var $convertPath;  //path for ImageMagick convert utility
	var $climate;  //splat climate code
}

//MAIN
/*
$fileName = "sample.omwpt";
$result = OMWPT_ConfigFileReader::read($fileName);
echo "Working directory: " . $result->workingDirectory . "\n";
echo "Transmitter filename: " . $result->transmitterFileName . "\n";
echo "Splat path: " . $result->splatPath . "\n";
echo "Convert path: " . $result->convertPath . "\n";
echo "Climate: " .  $result->climate . "\n";
*/
?>
